package com.lingrit.provider.utils;


import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.sql.*;
import java.util.*;

public class EtlDbUtils {
    private static Connection conn = null;
    private static Properties pro = new Properties();
    //加载驱动,使用静态块，只需要加载一次
    static {
        try {
            /**
             * 加载配置文件的两种方法，任选一种都可以
             */
            pro.load(EtlDbUtils.class.getClassLoader().getResourceAsStream("eltConfig/EtlDBUtil.properties"));
            String DRIVER = pro.getProperty("jdbc.driverClassName");
            Class.forName(DRIVER);
        } catch (IOException e) {

            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    //获取Connection连接对象的方法,使用static方便之后在其他类中调用
    public static Connection getConn() {
        try {
            conn = DriverManager.getConnection(
                    pro.getProperty("jdbc.uri"),
                    pro.getProperty("jdbc.username"),
                    pro.getProperty("jdbc.password"));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }
    /**
     * 查询sql。
     */
    public static List<Map<String, Object>> query(String sql) throws SQLException {
        Connection conn = getConn();
        PreparedStatement pstm = conn.prepareStatement(sql);
        try {
            return resultSetToList(pstm.executeQuery());
        } finally {
            closePs(pstm.executeQuery(), pstm, conn);
        }
    }

    /**
     * 功能：ResultSet 转为List<Map<String,Object>>
     */
    private static List<Map<String, Object>> resultSetToList(ResultSet rs)
            throws java.sql.SQLException {
        if (rs == null) {
            return Collections.EMPTY_LIST;
        }

        ResultSetMetaData md = rs.getMetaData(); // 得到结果集(rs)的结构信息，比如字段数、字段名等
        int columnCount = md.getColumnCount(); // 返回此 ResultSet 对象中的列数
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        Map<String, Object> rowData = new HashMap<String, Object>();
        while (rs.next()) {
            rowData = new HashMap<String, Object>(columnCount);
            for (int i = 1; i <= columnCount; i++) {
                rowData.put(md.getColumnName(i), rs.getObject(i));
            }
            list.add(rowData);
        }
        return list;
    }

    /**
     *得到表名
     */
    public static String getTableName(String sql) throws SQLException {
        Connection conn = getConn();
        String table_name = queryTableName(conn, sql);
        return table_name;

    }

    public static List queryTableColumn(Connection conn, String sql) {
        List column_name = new ArrayList();
        ResultSet rs = null;
        Statement stmt = null;
        ResultSetMetaData rsmd = null;

        try {
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);
            //获取字段名
            rsmd = rs.getMetaData();
            if (rsmd != null) {
                int count = rsmd.getColumnCount();
                for (int i = 1; i <= count; i++) {
                    column_name.add(rsmd.getColumnName(i));
                }
            }
            return column_name;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeSts(rs, stmt, conn);
        }
        return column_name;
    }

    public static String queryTableName(Connection conn, String sql) {

        ResultSet rs = null;
        Statement stmt = null;
        try {
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);
            String table_name = StringUtils.EMPTY;
            if(rs != null) {
                while (rs.next()) {
                    table_name = rs.getString("table_name");
                }
            }
            return table_name;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeSts(rs,stmt,conn);
        }
      return StringUtils.EMPTY;
    }

    //关闭资源的方法
    public  static void closePs(ResultSet rs, PreparedStatement ps, Connection conn) {
        if(rs!=null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(ps!=null) {
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(conn!=null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    //关闭资源的方法
    public  static void closeSts(ResultSet rs, Statement stmt, Connection conn) {
        if(rs!=null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(stmt!=null) {
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(conn!=null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
